package com.evan.petiymanta.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.AppUtils;
import com.bumptech.glide.Glide;
import com.evan.petiymanta.R;
import com.evan.petiymanta.model.ProjectFolder;
import com.evan.petiymanta.service.FileServiceHolder;
import com.evan.petiymanta.service.IFileExplorerService;
import com.google.android.material.chip.Chip;
import com.google.android.material.textview.MaterialTextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import lombok.Getter;

public class ProjectAdapter extends RecyclerView.Adapter<ProjectAdapter.ViewHolder> {
    @Getter
    private List<ProjectFolder> projects = new ArrayList<>();
    private final Context context;

    public ProjectAdapter(Context context) {
        this.context = context;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setProjects(List<ProjectFolder> projects) {
        this.projects = projects;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_project, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bind(projects.get(position));
    }

    @Override
    public int getItemCount() {
        return projects.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private final MaterialTextView titleText;
        private final MaterialTextView packageNameText;
        private final Chip versionText;
        private final ImageView iconImage;
        private final Button monitorButton;

        ViewHolder(View itemView) {
            super(itemView);
            titleText = itemView.findViewById(R.id.titleText);
            packageNameText = itemView.findViewById(R.id.packageNameText);
            versionText = itemView.findViewById(R.id.versionText);
            iconImage = itemView.findViewById(R.id.iconImage);
            monitorButton = itemView.findViewById(R.id.monitorButton);
        }

        @SuppressLint("DefaultLocale")
        void bind(ProjectFolder project) {
            titleText.setText(project.getTitle());
            packageNameText.setText(project.getPackageName());
            versionText.setText(String.format("v%s (%d)",
                    project.getVersionName(),
                    project.getVersionCode()));

            if (project.getIconPath() != null) {
                Glide.with(itemView)
                        .load(project.getIconPath())
                        .into(iconImage);
            }

            // 在点击时获取最新的 fileService
            monitorButton.setOnClickListener(v -> {
                IFileExplorerService fileService = FileServiceHolder.getFileService();
                if (fileService == null) {
                    Toast.makeText(context, "服务未连接", Toast.LENGTH_SHORT).show();
                    return;
                }

                try {
                    String projectPath = project.getPath();
                    String backupPath = new File(projectPath, ".backup/sdk").getAbsolutePath();
                    fileService.startMonitoring(projectPath, backupPath);
                    AppUtils.launchApp("com.iapp.app");
                    Toast.makeText(context, "已启动、请打包项目", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Log.e("ProjectAdapter", "Error starting monitoring", e);
                    Toast.makeText(context, "启动失败: " + e.getMessage(),
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
} 